package com.cqu.hhx.driver.service.impl;

import com.cqu.hhx.driver.entity.*;
import com.cqu.hhx.driver.entity.forWeb.Evaluation;
import com.cqu.hhx.driver.mapper.*;
import com.cqu.hhx.driver.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DJ
 * @create 2020-05-24 22:29
 * 订单服务实现
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private NewOrderMapper newOrderMapper;
    @Autowired
    private DoingOrderMapper doingOrderMapper;
    @Autowired
    private FinishOrderMapper finishOrderMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private DriverMapper driverMapper;


    /**
     * 乘客添加新的订单
     * @param newOrder
     * @return 0,1,2,3
     */
    @Override
    public Integer addNewOrder(NewOrder newOrder) {
        try {
            //是否已经存在订单判断
            NewOrder order=newOrderMapper.getOrder(newOrder.getCustomerId());
            if(order!=null){
                return 1;
            }else {
                Customer customer=customerMapper.getCustomerInfoById(newOrder.getCustomerId());
                //账户余额是否足够判断
                if(customer.getMoney()<newOrder.getPrice()){
                    return 3;
                }else {
                    newOrder.setPhone(customer.getPhone());
                    newOrderMapper.addNewOrder(newOrder);
                    return 0;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return 2;
        }
    }

    /**
     * 请求所有的新订单
     * @return
     */
    @Override
    public List<NewOrder> getAllNewOrder() {
        try {
            List<NewOrder> newOrders=newOrderMapper.getAllNewOrder();
            return newOrders;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 通过订单Id获取订单信息
     * @param orderId
     * @return
     */
    @Override
    public NewOrder getNewOrderByOrderId(Long orderId) {
        try {
            NewOrder newOrder=newOrderMapper.getOrderByOrderId(orderId);
            return newOrder;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过订单Id删除订单信息
     * @param orderId
     * @return 0:成功 1:失败
     */
    @Override
    public int deleteNewOrderByOrderId(Long orderId) {
        try{
            newOrderMapper.deleteOrderByOrderId(orderId);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 添加进行中订单
     * @param order
     * @return 0:成功 1:失败
     */
    @Override
    public Integer addDoingOrder(DoingOrder order) {
        try {
            doingOrderMapper.addOrder(order);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 乘客获取进行中订单
     * @param customerId
     * @return
     */
    @Override
    public DoingOrder customerGetDoingOrder(Long customerId) {
        try {
            DoingOrder order=doingOrderMapper.customerGetOrder(customerId);
            return order;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取乘客新下单订单
     * @param customerId
     * @return
     */
    @Override
    public NewOrder customerGetNewOrder(Long customerId) {
        try {
            NewOrder order=newOrderMapper.getOrder(customerId);
            return order;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 司机获取进行中订单
     * @param driverId
     * @return
     */
    @Override
    public DoingOrder driverGetDoingOrder(Long driverId) {
        try {
            DoingOrder order=doingOrderMapper.driverGetOrder(driverId);
            return order;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 订单完成后删除进行中订单
     * @param orderId
     * @return
     */
    @Override
    public int deleteDoingOrderById(Long orderId) {
        try {
            doingOrderMapper.deleteOrderById(orderId);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 添加已完成订单
     * @param finishOrder
     * @return
     */
    @Override
    public int addFinishOrder(FinishOrder finishOrder) {
        try{
            finishOrderMapper.addFinishOrder(finishOrder);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 
     * @param evaluation
     * @return
     */
    @Override
    public int evaluateOrder(Evaluation evaluation) {
        try {
            finishOrderMapper.updateOrderEvaluation(evaluation.getCustomerId(),
                    evaluation.getOrderId(),evaluation.getEvaluation());
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 乘客获取已完成订单
     * @param customerId
     * @return
     */
    @Override
    public List<FinishOrder> customerGetFinishOrders(Long customerId) {
        try {
            List<FinishOrder> finishOrders=finishOrderMapper.customerGetOrder(customerId);
            return finishOrders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 司机获取已完成订单
     * @param driverId
     * @return
     */
    @Override
    public List<FinishOrder> driverGetFinishOrders(Long driverId) {
        try {
            List<FinishOrder> finishOrders=finishOrderMapper.driverGetOrder(driverId);
            return finishOrders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除已完成订单
     * @param orderId
     * @return
     */
    @Override
    public int deleteFinishOrder(Long orderId) {
        try {
            finishOrderMapper.deleteOrder(orderId);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 计算司机评分平均值
     * @param driverId
     * @return
     */
    @Override
    public Float computeDriverAvaScore(Long driverId) {
        try {
            List<FinishOrder> orders=finishOrderMapper.driverGetOrder(driverId);
            if(orders!=null){
                Float score=0.0f;
                int count=0;
                for(FinishOrder order:orders){
                    score+=order.getEvaluation();
                    count++;
                }
                score=score/count;
                return score;
            }else{
                return 4.0f;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 4.0f;
        }
    }

    /**
     * 通过司机Id获取司机信息
     * @param driverId
     * @return
     */
    @Override
    public Driver getDriverInfoById(Long driverId) {
        try {
            return driverMapper.getDriverInfoById(driverId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 更新乘客账户余额
     * @param customerId
     * @param price
     * @param isAdd
     * @return
     */
    @Override
    public Float updateCustomerMoney(Long customerId, Float price, Boolean isAdd) {
        try {
            Customer customer=customerMapper.getCustomerInfoById(customerId);
            if(isAdd){
                Float money=customer.getMoney()+price;
                customerMapper.updateCustomerMoney(customerId,money);
                return money;
            }else{
                Float money=customer.getMoney()-price;
                customerMapper.updateCustomerMoney(customerId,money);
                return money;
            }
        }catch (Exception e){
            e.printStackTrace();
            return -1.0f;
        }
    }

    /**
     * 更新司机账户余额
     * @param driverId
     * @param price
     * @param isAdd
     * @return
     */
    @Override
    public Float updateDriverMoney(Long driverId, Float price, Boolean isAdd) {
        try{
            Driver driver=driverMapper.getDriverInfoById(driverId);
            if(isAdd){
                Float money=driver.getMoney()+price;
                driverMapper.updateDriverMoney(driverId,money);
                return money;
            }else{
                Float money=driver.getMoney()-price;
                driverMapper.updateDriverMoney(driverId,money);
                return money;
            }
        }catch (Exception e){
            e.printStackTrace();
            return -1.0f;
        }
    }

}
